<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="rooname">
      <el-input v-model="dataForm.rooname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="size">
      <el-input v-model="dataForm.size" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="thunb">
      <el-input v-model="dataForm.thunb" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="price">
      <el-input v-model="dataForm.price" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="description">
      <el-input v-model="dataForm.description" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="created">
      <el-input v-model="dataForm.created" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="content">
      <el-input v-model="dataForm.content" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lat">
      <el-input v-model="dataForm.lat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lng">
      <el-input v-model="dataForm.lng" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userid">
      <el-input v-model="dataForm.userid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pid">
      <el-input v-model="dataForm.pid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cid">
      <el-input v-model="dataForm.cid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="aid">
      <el-input v-model="dataForm.aid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="state">
      <el-input v-model="dataForm.state" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="address">
      <el-input v-model="dataForm.address" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cate">
      <el-input v-model="dataForm.cate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="price2">
      <el-input v-model="dataForm.price2" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          rooname: '',
          size: '',
          thunb: '',
          price: '',
          description: '',
          created: '',
          content: '',
          lat: '',
          lng: '',
          userid: '',
          username: '',
          pid: '',
          cid: '',
          aid: '',
          state: '',
          address: '',
          cate: '',
          price2: ''
        },
        dataRule: {
          rooname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          thunb: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lng: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          aid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          price2: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rooname = data.house.rooname
                this.dataForm.size = data.house.size
                this.dataForm.thunb = data.house.thunb
                this.dataForm.price = data.house.price
                this.dataForm.description = data.house.description
                this.dataForm.created = data.house.created
                this.dataForm.content = data.house.content
                this.dataForm.lat = data.house.lat
                this.dataForm.lng = data.house.lng
                this.dataForm.userid = data.house.userid
                this.dataForm.username = data.house.username
                this.dataForm.pid = data.house.pid
                this.dataForm.cid = data.house.cid
                this.dataForm.aid = data.house.aid
                this.dataForm.state = data.house.state
                this.dataForm.address = data.house.address
                this.dataForm.cate = data.house.cate
                this.dataForm.price2 = data.house.price2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'rooname': this.dataForm.rooname,
                'size': this.dataForm.size,
                'thunb': this.dataForm.thunb,
                'price': this.dataForm.price,
                'description': this.dataForm.description,
                'created': this.dataForm.created,
                'content': this.dataForm.content,
                'lat': this.dataForm.lat,
                'lng': this.dataForm.lng,
                'userid': this.dataForm.userid,
                'username': this.dataForm.username,
                'pid': this.dataForm.pid,
                'cid': this.dataForm.cid,
                'aid': this.dataForm.aid,
                'state': this.dataForm.state,
                'address': this.dataForm.address,
                'cate': this.dataForm.cate,
                'price2': this.dataForm.price2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
